package com.example.cloudpracticeplatform.controller;

import com.example.cloudpracticeplatform.common.Response;
import com.example.cloudpracticeplatform.config.MinioConfig;
import io.minio.Result;
import jakarta.annotation.Resource;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/common")
@CrossOrigin
public class CommonController {
    @Resource
    private MinioConfig minioConfig;

    @PostMapping("/upload")
    public Response upload(MultipartFile file){
        try {
            String url = minioConfig.putObject(file);
            return Response.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("上传失败");
        }
    }
    
    @PostMapping("/getUrl")
    public Response getUrl(@RequestParam String bucketName, @RequestParam String objectName){
        try {
            String url = minioConfig.getObjectUrl(bucketName, objectName);
            return Response.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("获取失败");
        }
    }
}
